package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.Forest.grm.AuthGRM;
import com.ruoyi.system.domain.Forest.grm.GRMData;
import com.ruoyi.system.service.GRMAuthClient;
import com.ruoyi.system.service.GRMLoadDataClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysDataBoardMapper;
import com.ruoyi.system.domain.SysDataBoard;
import com.ruoyi.system.service.ISysDataBoardService;

import javax.annotation.Resource;

/**
 * 数据统计Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-09
 */
@Service
public class SysDataBoardServiceImpl implements ISysDataBoardService 
{
    @Autowired
    private SysDataBoardMapper sysDataBoardMapper;

    @Resource
    private GRMLoadDataClient grmLoadDataClient;

    @Autowired
    private RedisCache redisCache;


    @Resource
    private GRMAuthClient grmAuthClient;


    /**
     * 查询数据统计
     * 
     * @param dataId 数据统计主键
     * @return 数据统计
     */
    @Override
    public SysDataBoard selectSysDataBoardByDataId(Long dataId)
    {
        return sysDataBoardMapper.selectSysDataBoardByDataId(dataId);
    }

    /**
     * 查询数据统计列表
     * 
     * @param sysDataBoard 数据统计
     * @return 数据统计
     */
    @Override
    public List<SysDataBoard> selectSysDataBoardList(SysDataBoard sysDataBoard)
    {
        return sysDataBoardMapper.selectSysDataBoardList(sysDataBoard);
    }

    /**
     * 新增数据统计
     * 
     * @param sysDataBoard 数据统计
     * @return 结果
     */
    @Override
    public int insertSysDataBoard(SysDataBoard sysDataBoard)
    {
        return sysDataBoardMapper.insertSysDataBoard(sysDataBoard);
    }

    /**
     * 修改数据统计
     * 
     * @param sysDataBoard 数据统计
     * @return 结果
     */
    @Override
    public int updateSysDataBoard(SysDataBoard sysDataBoard)
    {
        return sysDataBoardMapper.updateSysDataBoard(sysDataBoard);
    }

    /**
     * 批量删除数据统计
     * 
     * @param dataIds 需要删除的数据统计主键
     * @return 结果
     */
    @Override
    public int deleteSysDataBoardByDataIds(Long[] dataIds)
    {
        return sysDataBoardMapper.deleteSysDataBoardByDataIds(dataIds);
    }

    /**
     * 删除数据统计信息
     * 
     * @param dataId 数据统计主键
     * @return 结果
     */
    @Override
    public int deleteSysDataBoardByDataId(Long dataId)
    {
        return sysDataBoardMapper.deleteSysDataBoardByDataId(dataId);
    }
    /**
     * 统计
     * @param deptId
     * @param limit
     * @return
     */
    @Override
    public List<SysDataBoard> statistics(Long deptId, Integer limit){
        return sysDataBoardMapper.statistics(deptId,limit);
    }

    @Override
    public GRMData loadPlcData(Long deptId){
        if (redisCache.getCacheObject(CacheConstants.GRM_TOKEN_URL) == null) {
            AuthGRM authGRM = grmAuthClient.getToken();
            if (authGRM.getCode() == 0) {
                redisCache.setCacheObject(CacheConstants.GRM_TOKEN_URL, "http://" + authGRM.getAddr() + "/exdata?SID=" + authGRM.getSid() + "&OP=R");
                grmLoadDataClient.getData();
            }
        }
        GRMData grmData = grmLoadDataClient.refreshData();
        if (grmData.getCode() == 1){
            redisCache.setCacheObject(CacheConstants.GRM_TOKEN_URL, null);
        }
        return grmData;
    }
}
